package com.example.dao;

import com.example.entity.Cart;
import org.springframework.stereotype.Repository;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@Repository
public class CartDao {

    private Connection getConnection() throws SQLException {
        return DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/your_database_name?useSSL=false&serverTimezone=UTC",
                "your_username",
                "your_password"
        );
    }

    public List<Cart> listCart(Cart cart) {
        List<Cart> carts = new ArrayList<>();
        String sql = "SELECT * FROM cart WHERE userId = ?";
        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, cart.getUserId());
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Cart c = new Cart();
                c.setCartId(rs.getInt("cartId"));
                c.setFoodId(rs.getInt("foodId"));
                c.setShopId(rs.getInt("shopId"));
                c.setUserId(rs.getString("userId"));
                c.setQuantity(rs.getInt("quantity"));
                carts.add(c);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return carts;
    }

    public int saveCart(Cart cart) {
        String sql = "INSERT INTO cart (foodId, shopId, userId, quantity) VALUES (?, ?, ?, ?)";
        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
            pstmt.setInt(1, cart.getFoodId());
            pstmt.setInt(2, cart.getShopId());
            pstmt.setString(3, cart.getUserId());
            pstmt.setInt(4, cart.getQuantity());
            pstmt.executeUpdate();
            ResultSet rs = pstmt.getGeneratedKeys();
            if (rs.next()) {
                return rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public int updateCart(Cart cart) {
        String sql = "UPDATE cart SET quantity = ? WHERE foodId = ? AND shopId = ? AND userId = ?";
        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, cart.getQuantity());
            pstmt.setInt(2, cart.getFoodId());
            pstmt.setInt(3, cart.getShopId());
            pstmt.setString(4, cart.getUserId());
            return pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public int removeCart(Cart cart) {
        String sql = "DELETE FROM cart WHERE foodId = ? AND shopId = ? AND userId = ?";
        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, cart.getFoodId());
            pstmt.setInt(2, cart.getShopId());
            pstmt.setString(3, cart.getUserId());
            return pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public Cart getCart(Integer shopId, String userId, Integer foodId) {
        String sql = "SELECT * FROM cart WHERE shopId = ? AND userId = ? AND foodId = ?";
        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, shopId);
            pstmt.setString(2, userId);
            pstmt.setInt(3, foodId);
            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                Cart c = new Cart();
                c.setCartId(rs.getInt("cartId"));
                c.setFoodId(rs.getInt("foodId"));
                c.setShopId(rs.getInt("shopId"));
                c.setUserId(rs.getString("userId"));
                c.setQuantity(rs.getInt("quantity"));
                return c;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}